#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2024/10/25 14:57
# @Author    :hzy
# @FileName  :day.py
import uiautomator2 as u2
import numpy as np
import cv2, time


# python -m weditor


def my_screenshot(xy1, xy2, device, show=0, win_name="screenshot", color=cv2.COLOR_BGR2GRAY, thresh=127) -> np.ndarray:
    """
    :param xy1: 左上角坐标1
    :param xy2: 右下角坐标2
    :param device: 设备链接
    :param show: 是否展示
    :param win_name: 窗口名字
    :param color:返回格式（灰度or二值）
    :return: 根据提供的两个坐标进行截图后转换成灰度图（默认）
    """
    im = device.screenshot()
    num_img = np.asarray(im)
    gray = cv2.cvtColor(num_img, color)
    out = gray[xy1[1]:xy2[1], xy1[0]:xy2[0]]
    if show == 1:
        cv2.namedWindow(win_name, cv2.WINDOW_FREERATIO)
        cv2.imshow(win_name, out)
        cv2.waitKey()
    return out


def screen_find(A, B, template_way, device, num=10000000, sleep_time=5, try_my=False):
    # 加载模板图片
    template = cv2.imread(template_way, cv2.IMREAD_GRAYSCALE)
    min_loc, max_loc = (0, 0)
    max_val = 0
    while max_val < num:
        i = my_screenshot(A, B, device=device)
        res = cv2.matchTemplate(i, template, cv2.TM_CCOEFF)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        if try_my:
            return max_val
        time.sleep(sleep_time)
        print(f"置信度：{max_val / 170000:.2f}%")
    return min_loc, max_loc


def star(d):
    # 打开包体
    d.app_start("com.yongshi.tenojo.ys")
    # 检测屏幕是否启动成功后
    screen_find((1189, 438), (1272, 535), './signOut.jpg', device=d)
    print("启动成功")
    time.sleep(5)  # 等待加载
    # 点击屏幕进入游戏
    for i in range(20):
        d.click(x=1136, y=440)
        time.sleep(1)
    for i in range(5):
        d.click(x=128, y=552)
        time.sleep(1)


def collect_stamina(d):
    # 领取每日的那个60体力
    d.click(701, 30)
    time.sleep(1)
    d.click(875, 117)
    time.sleep(0.5)
    d.click(701, 429)
    time.sleep(1)
    d.click(975, 409)
    time.sleep(1)
    d.click(215, 658)
    pass


def go_shopping(d):
    d.click(727, 666)
    time.sleep(1)
    d.click(358, 630)
    time.sleep(1)
    d.click(120, 120)
    time.sleep(1)
    d.click(532, 112)
    time.sleep(1)
    d.click(384, 278)
    time.sleep(1)
    d.click(386, 271)
    time.sleep(1)
    d.click(885, 606)
    time.sleep(1)
    # 返回主页
    for i in range(5):
        d.click(401, 58)
        time.sleep(0.5)
    d.click(122, 38)


def LFXY(d):
    # 联防协议
    d.click(1134, 194)
    time.sleep(1)
    _, c = screen_find((0, 0), (230, 686), "./lfxy.jpg", device=d, sleep_time=1)
    d.click(c[0] + 75, c[1] + 20)
    time.sleep(1)
    d.click(1146, 668)
    time.sleep(1.5)
    d.click(963, 666)
    time.sleep(1)
    d.click(650, 442)
    time.sleep(1)
    for i in range(2):
        d.click(936, 573)
        time.sleep(1)
        d.click(1231, 563)
        time.sleep(1)
        d.click(993, 676)
        time.sleep(1)
        d.click(896, 604)
        time.sleep(5)
        d.click(1167, 671)
        time.sleep(1)
    d.click(130, 35)
    pass


def receive_LFXY(d):
    # 领取协议
    d.click(1134, 194)
    time.sleep(0.2)
    _, c = screen_find((0, 0), (230, 686), "./lfxy.jpg", device=d, sleep_time=1)
    d.click(c[0] + 90, c[1] + 15)
    time.sleep(1)
    d.click(1203, 596)
    time.sleep(1)
    d.click(1203, 596)
    time.sleep(1)
    d.click(128, 35)
    time.sleep(1)
    pass


def receive_MMJ(d):
    # 领取喵喵机
    d.click(1195, 81)
    time.sleep(1)
    d.click(1059, 312)
    time.sleep(0.2)
    d.click(967, 650)
    time.sleep(2)
    for i in range(20):
        d.click(1182, 135)
        time.sleep(0.2)
    d.click(972, 658)
    time.sleep(1.5)
    d.click(1169, 622)
    time.sleep(0.3)
    d.click(931, 355)
    time.sleep(5)
    d.click(1198, 217)
    time.sleep(5)
    d.click(133, 30)
    pass


def receive_MRWT(d):
    # 领取每日委托
    for i in range(10):
        d.click(650, 225)
        time.sleep(1)
    time.sleep(1)
    d.click(1047, 86)
    time.sleep(1)
    for i in range(5):
        d.click(1131, 666)
        time.sleep(1)
    d.click(115, 209)
    for i in range(6):
        d.click(1131, 666)
        time.sleep(1)
    # 注意：不一定回到主页，建议直接退出
    pass


def extraction(d):
    # 每日萃取
    d.click(1177, 624)
    time.sleep(5)
    d.click(778, 653)
    time.sleep(5)
    d.swipe_ext("left", 0.5)
    time.sleep(5)
    d.click(650, 355)
    time.sleep(1)
    d.click(1024, 666)
    time.sleep(2)
    d.click(1139, 676)
    time.sleep(180)
    d.click(1172, 663)
    time.sleep(5)
    d.click(122, 653)
    time.sleep(5)
    d.click(1029, 666)
    time.sleep(2)
    d.click(1029, 666)
    time.sleep(2)
    d.click(1141, 61)
    time.sleep(2)
    d.click(125, 33)


def labourUnion(d):
    # 领取工会奖励
    d.click(826, 655)
    time.sleep(3)
    d.click(1180, 671)
    time.sleep(0.3)
    d.click(629, 506)
    time.sleep(0.3)
    d.click(629, 506)
    time.sleep(1)
    d.click(885, 640)
    time.sleep(1)
    d.click(122, 40)
    # (1162, 140) (1218, 197)


def pq(d):
    d.click(348, 161)
    time.sleep(0.5)
    d.click(808, 586)
    time.sleep(0.5)


def hometown(d):
    # 领取游园街
    d.click(1008, 660)
    time.sleep(3)
    d.click(1008, 660)
    time.sleep(0.5)
    d.click(1008, 660)
    time.sleep(0.5)
    d.click(860, 655)
    time.sleep(0.5)
    d.click(1039, 324)
    time.sleep(3)
    d.click(348, 424)
    time.sleep(3)
    d.click(634, 653)
    time.sleep(0.5)
    d.click(220, 601)
    time.sleep(0.5)
    pq(d)
    d.click(619, 612)
    time.sleep(0.5)
    pq(d)
    d.click(995, 601)
    time.sleep(0.5)
    pq(d)
    d.swipe_ext("left", 0.5)
    time.sleep(5)
    d.click(1041, 614)
    time.sleep(0.5)
    pq(d)
    d.click(773, 681)
    time.sleep(0.5)
    d.click(773, 681)
    time.sleep(0.5)
    d.click(51, 38)
    time.sleep(0.5)
    d.click(51, 38)


def collectAgreement(d):
    # 领取协议
    d.click(46, 117)
    time.sleep(1)
    d.click(46, 117)
    time.sleep(0.5)
    d.click(46, 117)
    time.sleep(0.5)
    d.click(102, 671)
    time.sleep(0.5)
    d.click(1146, 681)
    time.sleep(0.5)
    d.click(1146, 681)
    time.sleep(0.5)
    d.click(56, 38)
    time.sleep(0.5)
    d.click(1154, 666)
    time.sleep(0.5)
    d.click(30, 40)
    time.sleep(0.5)
    d.click(30, 40)
    time.sleep(1)


if __name__ == "__main__":
    # 连接模拟器
    equipment = u2.connect('127.0.0.1:16384')
    # 启动游戏
    print("启动游戏")
    star(d=equipment)
    time.sleep(5)
    # 收集体力
    print("收集体力")
    collect_stamina(d=equipment)
    time.sleep(5)
    # 领取每日商店
    print("商店")
    go_shopping(d=equipment)
    time.sleep(5)
    # ========================================
    # 清理体力
    print("体力")
    LFXY(d=equipment)
    time.sleep(5)  # 联防护协议
    equipment.app_stop("com.yongshi.tenojo.ys")
    time.sleep(10)
    # ========================================
    # 每日萃取
    # star(d=equipment)
    print("萃取")
    extraction(d=equipment)
    time.sleep(5)
    equipment.app_stop("com.yongshi.tenojo.ys")
    time.sleep(10)
    # 领取活动
    star(d=equipment)
    print("活动")
    receive_LFXY(d=equipment)
    time.sleep(5)
    equipment.app_stop("com.yongshi.tenojo.ys")
    time.sleep(10)
    # 领取喵喵
    star(d=equipment)
    print("喵喵")
    receive_MMJ(d=equipment)
    time.sleep(5)
    equipment.app_stop("com.yongshi.tenojo.ys")
    time.sleep(10)
    # 领取工会奖励
    print("工会")
    star(d=equipment)
    time.sleep(5)
    labourUnion(d=equipment)
    equipment.app_stop("com.yongshi.tenojo.ys")
    # 领取家园奖励
    time.sleep(5)
    star(d=equipment)
    print("家园")
    hometown(d=equipment)
    time.sleep(5)
    # 领取每日
    print("委托")
    receive_MRWT(d=equipment)
    time.sleep(5)
    equipment.app_stop("com.yongshi.tenojo.ys")
    time.sleep(10)
    # 领取协议
    print("大月卡")
    star(d=equipment)
    time.sleep(5)
    collectAgreement(d=equipment)
    time.sleep(5)
    pass
